'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, TEXT } = app.Sequelize;
  const OrderGoods = app.model.define(
      'orderGoods',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
          order_no: STRING(255),
          order_type: STRING(255),
          member_id: INTEGER(11),
          status: INTEGER(11),
          order_status: INTEGER(11),
          pay_status: INTEGER(11),
          pay_type: STRING(255),
          back_reason: STRING(255),
          express_code: STRING(255),
          express_no: STRING(255),
          express_info: TEXT(),
          express_status: INTEGER(11),
          address_id: INTEGER(11),
          day: STRING(255),
          time: STRING(255),
          coupon_id: INTEGER(11),
          coupon_member_id: INTEGER(11),
          export_status: INTEGER(11),
          remark: STRING(255),
          create_time: {
              type: app.Sequelize.DATE,
              allowNull: false,
              get() {
                  return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          updated_time: {
              type: app.Sequelize.DATE,
              allowNull: false,
              get() {
                  return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          }
      },
      {
          tableName: 'order',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    OrderGoods.associate = function () {
        OrderGoods.hasMany(app.model.OrderGoodsDetail, { foreignKey: 'order_id', targetKey: 'id'})
        OrderGoods.belongsTo(app.model.Member, { foreignKey: 'member_id', targetKey: 'id'})
        OrderGoods.belongsTo(app.model.Address, { foreignKey: 'address_id', targetKey: 'id'})
        OrderGoods.belongsTo(app.model.Coupon, { foreignKey: 'coupon_id', targetKey: 'id'})
    }
    return OrderGoods
};
